#include "sqlconn.h"

CSqlConn::CSqlConn()
{
    m_conn = mysql_init(nullptr);
    mysql_set_character_set(m_conn, "utf8");
}
CSqlConn::~CSqlConn()
{
    if (nullptr != m_conn)
    {
        mysql_close(m_conn);
    }
}

bool CSqlConn::connect( const std::string& ip,
                       const uint16_t port,
                       const std::string& user,
                       const std::string& pwd,
                       const std::string& dbName)
{
    MYSQL* ptr = mysql_real_connect(m_conn, ip.c_str(), user.c_str(), pwd.c_str(), dbName.c_str(), port, nullptr, 0);
    return ptr != nullptr;
}
bool CSqlConn::update(const std::string& sql)
{
    if(mysql_query(m_conn, sql.c_str()))
    {
        return false;
    }
    return true;
}

MYSQL_RES* CSqlConn::query(const std::string& sql)
{
    if(mysql_query(m_conn, sql.c_str()))
    {
        return nullptr;
    }
    return mysql_use_result(m_conn);
}